package com.xinnet.mms.allot.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xinnet.core.dao.impl.BaseDaoImpl;
import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.allot.dao.CustomAllotConfigDao;
import com.xinnet.mms.entity.CustomAllotConfig;
import com.xinnet.mms.entity.CustomAllotConfigDept;
import com.xinnet.mms.entity.Dept;

@Repository("customAllotConfigDao")
public class CustomAllotConfigDaoImpl extends BaseDaoImpl<CustomAllotConfig, Long> implements CustomAllotConfigDao {

	//通过部门id查询该部门的分配轮循设置
	@Override
	public List<CustomAllotConfig> showAllotConfigByDeptno(String deptno) throws SQLException {
		return this.getSqlSession().selectList("customerallot.showAllotConfigByDeptno", deptno);
	}

	//显示客户分配设置页面部门 以及所属分公司
	@Override
	public PageView<CustomAllotConfigDept> queryAllotConfig(PageView<CustomAllotConfigDept> pageView) throws SQLException {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("paging", pageView);
		pageView.setRecords(this.getSqlSession().selectList("customerallot.queryAllotConfig",map));
		return pageView;
	}

	//用组织结构标识和上级组织结构标识查询分配设置页面部门以及所属分公司
	@Override
	public PageView<CustomAllotConfigDept> queryAllotConfig(PageView<CustomAllotConfigDept> pageView,CustomAllotConfigDept configDept) throws SQLException {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("paging", pageView);
		map.put("t", configDept);
		pageView.setRecords(this.getSqlSession().selectList("customerallot"+".queryAllotConfigByDeptno",map));
		return pageView;
	}

	//修改部门分配轮循设置
	@Override
	public void updateAllotConfig(CustomAllotConfig allotConfig) throws SQLException {
		this.getSqlSession().update("customerallot.updateAllotConfig",allotConfig);
	}

	//根据部门名称过滤查询分配设置列表
	@Override
	public PageView<CustomAllotConfigDept> queryAllotDeptByDname(PageView<CustomAllotConfigDept> pageView,Dept dept) throws SQLException {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("paging", pageView);
		map.put("t", dept);
		pageView.setRecords(this.getSqlSession().selectList("customerallot"+".queryAllotDeptByDname",map));
		if(pageView.getRecords().isEmpty())
			return null;
		return pageView;
	}

	//通过部门编号 将该部门周一到周五的分配设置都设为不启用审核状态
	@Override
	public void updateAllotConfigCheckResult(CustomAllotConfig allotConfig) throws SQLException {
		this.getSqlSession().update("customerallot.updateAllotConfigCheckResult",allotConfig);
	}

	/**添加部门时增加初始化客户分配规则*/
	@Override
	public void addDeptDsitributRule(CustomAllotConfig allotConfig) throws SQLException {
		this.getSqlSession().insert("config.addDeptDsitributRule",allotConfig);
	}
	
	public List<CustomAllotConfigDept> getCustomDsitributRuleByDeptIdStr(String deptIdStr){
		List<CustomAllotConfigDept> customAllotConfigDeptList = this.getSqlSession().selectList("customerallot.getCustomDsitributRuleByDeptIdStr",deptIdStr);
		return customAllotConfigDeptList;
	}
	
	/**
	 * 获取客户分配规则
	 * @param dname
	 * @return
	 */
	public List<CustomAllotConfigDept> getCustomDsitributRuleByDname(String dname){
		List<CustomAllotConfigDept> customAllotConfigDeptList = this.getSqlSession().selectList("customerallot.getCustomDsitributRuleByDname",dname);
		return customAllotConfigDeptList;
	}

}
